<?php

namespace app\common\controller;

use app\BaseController;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\App;
use think\facade\Filesystem;

class Upload2 extends BaseController
{
    function __construct(App $app){
        parent::__construct($app);
    }

    /**
     * 上传base64文件
     */
    public function upload_base64_file(): bool|string
    {
        try {
            $file = $this->request->param('file');
            if(empty($file)) throw new \Exception('请选择文件');
            // 上传单文件
            $upload = new \app\common\Upload();
            // 设置上传类型和文件大小
            $upload = $upload->local()->size(50);
            $ext = '.png';
            if (is_array($file)){
                $src = [];
                foreach ($file as $v){
                    $save_name = '/upload/'.date('Ym').'/'.date('d').'_'.time().uniqid();
                    $src[] = $save_name.$ext;
                    $upload->upload($v,'base64',$ext,'/public'.$save_name);
                }
            }else{
                $save_name = '/upload/'.date('Ym').'/'.date('d').'_'.time().uniqid();
                $src = $save_name.$ext;
                $upload->upload($file,'base64',$ext,'/public'.$save_name);
            }
            return success($src);
        } catch (\Exception $e){
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 上传文件
     */
    public function upload_file(): bool|string
    {
        try {
            $file = $this->request->file('file');
            if(empty($file)) throw new \Exception('请选择文件');
            // 上传操作
            if(is_array($file)) {
                // 上传多文件
                $src = [];
                foreach ($file as $v) {
                    $src[] = '/storage/'.Filesystem::disk('public')->putFile( 'file', $v);
                }
            }else {
                // 上传单文件
                $src = '/storage/'.Filesystem::disk('public')->putFile( 'file', $file);
            }
            return success($src);
        } catch (\Exception $e){
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 上传量表文件
     * @return false|string
     */
    public function uploadRatingScaleFile(): bool|string
    {
        try {
            $file = $this->request->file('file');
            if(empty($file)) throw new \Exception('请选择文件');
            // 上传操作
            if(is_array($file)) {
                // 上传多文件
                $src_list = [];
                $path = './public';
                foreach ($file as $v) {
                    $src = '/storage/'.Filesystem::disk('public')->putFile( 'scale', $v);
                    $file_url = str_replace('\\','/',$path . $src);
                    $spreadSheet = IOFactory::load($file_url);
                    $sheet = $spreadSheet->getSheet(0);
                    $RatingScaleName = $sheet->getCell('B2')->getValue();
                    $src_list[] = ['src' => $file_url, 'rating_scale_name' => $RatingScaleName];
                }
            }else {
                // 上传单文件
                $path = './public';
                $src = '/storage/'.Filesystem::disk('public')->putFile( 'scale', $file);
                $file_url = str_replace('\\','/',$path . $src);
                $spreadSheet = IOFactory::load($file_url);
                $sheet = $spreadSheet->getSheet(0);
                $RatingScaleName = $sheet->getCell('B2')->getValue();
                $src_list = ['src' => $file_url, 'rating_scale_name' => $RatingScaleName];
            }
            return success($src_list);
        } catch (\Exception $e){
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 上传用户文件
     * @return false|string
     */
    public function uploadUserFile(): bool|string
    {
        try {
            $file = $this->request->file('file');
            if(empty($file)) throw new \Exception('请选择文件');
            // 上传操作
            if(is_array($file)) {
                // 上传多文件
                $src = [];
                foreach ($file as $v) {
                    $src[] = '/storage/'.Filesystem::disk('public')->putFile( 'user_file', $v);
                }
            }else {
                // 上传单文件
                $src = '/storage/'.Filesystem::disk('public')->putFile( 'user_file', $file);
            }
            return success(['src' => $src]);
        } catch (\Exception $e){
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }


}
